package com.enjar.plugins;

import java.util.Random;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
	private boolean ledShow = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main2);
		init();
	}
	
	
	public void init() {
		JBInterface.initPrinter();
		GpioControl.activateLed();
		GpioControl.activatePrinter();
		activateLed();
	}
	
	
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dayin_gb2312:
			
			activatePrinter();
			
			PrintTools_58mm.printTextCenterCmd();
			String ch = getResources().getString(R.string.chinese);
			
			PrintTools_58mm.printText("地址：厦门市区里去万达广场1#", ch);
			PrintTools_58mm.printText("电话：18659264592", ch);
			PrintTools_58mm.printText("单号：198710092871", ch);
//			
//			PrintTools_58mm.writeEnterLine(1);
			PrintTools_58mm.printText("---------------------------", ch);
		
			
			PrintTools_58mm.setLocation();
			PrintTools_58mm.printTextCenter("产品名称", ch);	
			PrintTools_58mm.printTextCenter("数量", ch);				
			PrintTools_58mm.printTextCenter("单价", ch);
//			PrintTools_58mm.print(PrintTools_58mm.LF);
			
			PrintTools_58mm.printTextCenter("苹果", ch);		
			PrintTools_58mm.printTextCenter("2", ch);			
			PrintTools_58mm.printTextCenter("10", ch);
//			PrintTools_58mm.print(PrintTools_58mm.LF);
			
			PrintTools_58mm.printTextCenter("香蕉", ch);		
			PrintTools_58mm.printTextCenter("1", ch);
			PrintTools_58mm.printTextCenter("5", ch);
			
			PrintTools_58mm.printTextCenter("草莓蛋糕", ch);		
			PrintTools_58mm.printTextCenter("1", ch);
			PrintTools_58mm.printTextCenter("5", ch);
			
			PrintTools_58mm.printTextCenter("地瓜粉", ch);		
			PrintTools_58mm.printTextCenter("1", ch);
			PrintTools_58mm.printTextCenter("15", ch);
			
			PrintTools_58mm.printTextCenter("蜜橘", ch);		
			PrintTools_58mm.printTextCenter("4", ch);
			PrintTools_58mm.printTextCenter("25", ch);
			
//
			PrintTools_58mm.printText("---------------------------", ch);
//			PrintTools_58mm.writeEnterLine(1);
			
			PrintTools_58mm.printText("合计：125", ch);
			PrintTools_58mm.printText("应收金额：115", ch);			
			PrintTools_58mm.printText("实收金额：120", ch);
			PrintTools_58mm.printText("找零：5", ch);
			PrintTools_58mm.printText("2013-10-30 18:34:56", ch);
			PrintTools_58mm.writeEnterLine(1);
			/**
			 * 	
			 	产品名称         数量         单价
				苹果                   2     10
				香蕉                   1     7
			 */
			
			break;
		case R.id.cashbox:
			cashBoxAuto();
			break;
		case R.id.cust_display:
			custDisplay();
			break;
		default:
			break;
		}
	}
	
	
	
	public void activateLed() {
		if (!ledShow) {
			ledShow = true;
			int convertLed = GpioControl.convertLed();
			toastLong("切换客显 " + ((convertLed < 0)?"失败 X":"成功 √"));
			C.ledSerialPortTools = new SerialPortTools(C.ledPort, C.ledBaudrate);
		}
	}

	public void activatePrinter() {
		if(ledShow){	
			ledShow = false;
			int convertPrinter = GpioControl.convertPrinter();
			toastLong("切换打印机 " + ((convertPrinter < 0)?"失败 X":"成功 √"));
			C.kitchenSerialPortTools = new SerialPortTools(C.kitchenPort, C.kitchenBaudrate);
		}
	}
	
	
	public void custDisplay() {
		activateLed();
		Random r = new Random();
		LedTools.printNums(r.nextDouble() + "");
	}
	
	
	public void cashBoxAuto(){
		System.err.println("box-打开钱箱!");
		GpioControl.activate(GpioControl.qx_o, true);
		new Handler().postDelayed(new Runnable(){    
		    public void run() {
		    	System.err.println("box-钱箱关闭");
		    	GpioControl.activate(GpioControl.qx_o, false);
		    }    
		 }, 500);
	}
	
	
	public void toastLong(String msg)
	{
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	
	
	@Override
	protected void onDestroy() {
		JBInterface.closePrinter();
		super.onDestroy();
	}

}
